package com.edward.action;

import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;
import org.springframework.security.annotation.Secured;

import com.edward.JsonDateProcessor;
import com.edward.JsonTimeProcessor;
import com.edward.PagerModel;
import com.edward.model.OverseaTax;
import com.edward.service.OverseaTaxManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class OverseaTaxAction extends ActionSupport{


	private static final long serialVersionUID = 6967564864734915097L;

	private OverseaTax overseaTax;
	
	private OverseaTaxManager overseaTaxManager;
	
	private String query="";
	
	private int limit;
	
	private int start;
	
	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}
	
	public OverseaTax getOverseaTax() {
		return overseaTax;
	}

	public void setOverseaTax(OverseaTax overseaTax) {
		this.overseaTax = overseaTax;
	}

	public void setOverseaTaxManager(OverseaTaxManager overseaTaxManager) {
		this.overseaTaxManager = overseaTaxManager;
	}

	@Secured("ROLE_COREFINANCE_ADD")
	public String add() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		OverseaTax o = overseaTax;
		overseaTax.setSum(o.getTax1() + o.getTax2() + o.getTax3());
		overseaTaxManager.addOverseaTax(overseaTax);
		overseaTaxManager.addcheck(overseaTax);
		response.getWriter().print("{success : true , msg : '添加成功!'}");
		return null;
	}
	
	@Secured("ROLE_COREFINANCE_UPDATE")
	public String update() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		OverseaTax ot = overseaTax;
		overseaTax.setSum(ot.getTax1() + ot.getTax2() + ot.getTax3());
		OverseaTax otax = overseaTaxManager.getOverseaTax(overseaTax.getId());
		OverseaTax o = overseaTaxManager.getSum(ot.getDate());
		o.setTax1(o.getTax1() + ot.getTax1() - otax.getTax1());
		o.setTax2(o.getTax2() + ot.getTax2() - otax.getTax2());
		o.setTax3(o.getTax3() + ot.getTax3() - otax.getTax3());
		o.setSum(o.getSum()+ot.getSum() - otax.getSum());
		overseaTaxManager.updateOverseaTax(overseaTax);
		overseaTaxManager.updateOverseaTax(o);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_COREFINANCE_UPDATE")
	public String delete() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		OverseaTax otax = overseaTaxManager.getOverseaTax(overseaTax.getId());
		OverseaTax o = overseaTaxManager.getSum(otax.getDate());
		o.setTax1(o.getTax1() - otax.getTax1());
		o.setTax2(o.getTax2() - otax.getTax2());
		o.setTax3(o.getTax3() - otax.getTax3());
		o.setSum(o.getSum()- otax.getSum());
		overseaTaxManager.updateOverseaTax(o);
		overseaTaxManager.deleteOverseaTax(overseaTax.getId());
		response.getWriter().print("{success : true , msg : '删除成功!'}");
		return null;
	}

	@Secured("ROLE_COREFINANCE")
	public String getList() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
	    JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonTimeProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonTimeProcessor());
		PagerModel pager;
	    pager = overseaTaxManager.pagerList(start, limit,query);
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	@Secured("ROLE_COREFINANCE")
	public String getSumList() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
	    JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
	    pager = overseaTaxManager.pagerSumList(start, limit,query);
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	
	@Secured("ROLE_COREFINANCE")
	 public String getListJudge() throws Exception {
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
				response.getWriter().print("{success : true , msg : '成功!',deny:false}");
		 return null;
	 }
	
	
	
	
	
	
	

}
